<?php
/**
 * 多文件上传
 *
 * @license       http://www.1wedo.com/
 */

class UploadifyController extends CommonsController{

    /**
     * 基本上传
     *
     * @return [type] [description]
     */
    public function actionBasic() {
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $this->layout = '/layouts/dialog';
        $this->render( 'basic' );
    }

    public function actionBasicAll(){
        $num = $_GET['num'];
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $this->layout = '/layouts/dialog';
        $this->render( 'basic_all' ,array('num'=>$num));
    }



    public function actionFile(){
        $this->layout = '/layouts/dialog';
        Yii::app()->clientScript->registerScriptFile($this->adminPath . '/js/jquery.min.js',CClientScript::POS_HEAD);
        $type = $_GET['t'];
        $this->render( 'file',array('type'=>$type));
    }
    /**
     *       单文件上传
     */
    public function actionBasicExecute() {
        if ( XUtils::method() == 'POST' ) {
            $adminiUserId = yii::app()->admin->getState('admin_id');
            if(!$adminiUserId){
                $adminiUserId = 0;
            }
            $file = XUpload::upload($_FILES['imgFile'], array('thumb'=>false, 'thumbSize'=>array ( 720 , 330 ),'pathurl'=>'./uuploads/','allowExts' => 'jpg,gif,png,jpeg' ) );
            if ( is_array( $file ) ) {
                $model = new ArticleUpload();
                $model->userid = intval( $adminiUserId );
                $model->real_name = $file['name'];
                $model->file_name = substr($file['pathname'],2);
                $model->save_path = substr($file['savepath'],2);
                $model->thumb = substr($file['paththumbname'],2);
                $model->save_name = $file['savename'];
                $model->create_time = time();
                $model->hash = $file['hash'];
                $model->file_ext = $file['extension'];
                $model->file_mime = $file['type'];
                $model->file_size = $file['size'];
                $model->down_count = 0;
                $model->access = '';
                if ( $model->save() ) {
                    exit( CJSON::encode( array ( 'state' => 'success' , 'fileId'=>$model->id, 'realFile'=>$model->real_name, 'message' => '上传成功' , 'file' =>  $model->file_name ) ) );
                } else {
                    @unlink( $file['pathname'] );
                    exit( CJSON::encode( array ( 'state' => 'error' , 'message' => '数据写入失败，上传错误' ) ) );
                }

            } else {
                exit( CJSON::encode( array ( 'error' => 1 , 'message' => '上传错误' ) ) );
            }
        }
    }
    /**
     * 上传文件
     */
    public function actionFileExecute(){
        if ( XUtils::method() == 'POST' ) {
            $filetype = $_POST['filetype'];
            $adminiUserId = yii::app()->admin->getState('user_id');
            $file = XUpload::upload($_FILES['imgFile'], array('pathurl'=>'./uuploads/','allowExts' => $filetype ,'maxSize' => 32922000 ) );
                exit( CJSON::encode( array ( 'state' => 'success' , 'fileId'=>'', 'realFile'=>$file['name'], 'message' => '上传成功'
                , 'file' =>  substr($file['pathname'],2)) ) );
        } else {
            exit( CJSON::encode( array ( 'error' => 1 , 'message' => '上传错误' ) ) );
        }

    }



    /**
     * 编辑器文件上传
     */
    public function actionContentUpload ()
    {
        if (XUtils::method() == 'POST') {
            $accountUserId = Yii::app()->admin->getState('admin_id');
            if(!$accountUserId){
                $accountUserId = 0;
            }
            $file = XUpload::upload($_FILES['imgFile'], array('thumb'=>false, 'thumbSize'=>array ( 360 , 200 ),'pathurl'=>'./uuploads/','allowExts' => 'jpg,gif,png,jpeg' ) );
            if (is_array($file)) {
                $model = new ArticleUpload();
                $model->userid = intval( $accountUserId );
                $model->real_name = $file['name'];
                $model->file_name = substr($file['pathname'],2);
                $model->save_path = substr($file['savepath'],2);
                $model->thumb = substr($file['paththumbname'],2);
                $model->save_name = $file['savename'];
                $model->create_time = time();
                $model->hash = $file['hash'];
                $model->file_ext = $file['extension'];
                $model->file_mime = $file['type'];
                $model->file_size = $file['size'];
                $model->down_count = 0;
                $model->access = '';
                if ( $model->save() ) {
                    exit(CJSON::encode(array ('error' => 0 , 'url' => Yii::app()->homeUrl . substr($file['pathname'],2))));
                } else {
                    @unlink( $file['pathname'] );
                    exit(CJSON::encode(array ('error' => 1 , 'message' => '上传错误' )));
                }
            } else {
                exit(CJSON::encode(array ('error' => 1 , 'message' => '上传错误' )));
            }
        }
    }









    /**
     * 删除附件
     * @return [type] [description]
     */
    public function actionRemove() {
        $imageId = intval($_POST['imageId']);
        try {
            $imageModel = ArticleUpload::model()->findByPk( $imageId );
            if ( $imageModel ==false )
                throw new Exception( "附件已经被删除" );
            @unlink( $imageModel->file_name );
            @unlink( $imageModel->thumb );
            if ( !$imageModel->delete() )
                throw new Exception( "数据删除失败" );
            $var['state'] = 'success';
            $var['message'] = '删除完成';
        } catch ( Exception $e ) {
            $var['state'] = 'errro';
            $var['message'] = '失败：'.$e->getMessage();
        }
        exit( CJSON::encode( $var ) );
    }









}
